package com.xiaoqiang.proxy.cglib;

import com.xiaoqiang.proxy.Hello;
import com.xiaoqiang.proxy.HelloDefault;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author 小强
 * @since 2019/1/3 23:00
 */
public class CGLIBProxy implements MethodInterceptor {

    public static void main(String[] args) {
        Hello hello = new HelloDefault();
        CGLIBProxy proxy = new CGLIBProxy();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(hello.getClass());
        enhancer.setCallback(proxy);

        hello = (Hello) enhancer.create();
        hello.say("李四");

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before invoke");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("after invoke");
        return result;
    }
}
